package me.healthcare.webserivce;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import me.healthcare.common.NumberGenerator;

import org.json.JSONObject;

import android.os.AsyncTask;

public class RegisterService extends AsyncTask<Void, Void, Void> {

	private boolean isSuccess;

	private String email;
	private String pass;
	private String fullname;
	private String sex;
	private String dienthoai;
	private String diachi;

	private String executeURL;
	private final String PATIENT_URL = "/sworldwebservice/sworld/auth/InsertPatients/12345/"
			+ pass
			+ "/"
			+ email
			+ "/"
			+ fullname
			+ "/"
			+ sex
			+ "/"
			+ dienthoai
			+ "/" + diachi;
	private final String DOCTOR_URL = "?";

	@Override
	protected Void doInBackground(Void... params) {
		URL jsonURL;
		URLConnection jc;
		try {
			jsonURL = new URL("http://" + WebService.HOST_IP + executeURL);

			jc = jsonURL.openConnection();
			InputStream is = jc.getInputStream();

			BufferedReader reader = new BufferedReader(new InputStreamReader(
					is, "iso-8859-1"), 8);
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			is.close();
			String jsonTxt = sb.toString(); // doc StringBuilder vao chuoi
			JSONObject jj = new JSONObject(jsonTxt);
			String status = jj.getString("status");

			if (status.equalsIgnoreCase("SUCCESS")) {
				isSuccess = true;
			}
		} catch (Exception e) {
		}
		return null;
	}

	public boolean patientRegister(String fullname, String email,
			String password, String acvite_key) {
		this.email = email;
		this.fullname = fullname;
		this.pass = password;

		sex = "Nam";
		dienthoai = "0" + NumberGenerator.getIntNumber(1641691924, 1649691924);
		diachi = "trống";

		isSuccess = false;
		executeURL = PATIENT_URL;
		this.execute();
		return isSuccess;
	}
	// TODO

}
